<?php

namespace app\wechat\controller;

use think\Db;

class Allow extends Base
{
    //待审批的列表
    public function AllowList()
    {
        $orderInfo = model('order')
            ->where('status', 4)
            ->order('create_time','desc')
            ->select();
        $viewData  = [
            'order' => $orderInfo
        ];
        $this->assign($viewData);
        return view();
    }

    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function Allow()
    {
        $user = session("user");
        if (request()->isAjax()) {
            $data         = input('post.');
            $order_status = Db::name('order')->where('order_number', $data['order_number'])->find();
            if ($order_status['allow_status']) {
                $this->error('该订单已经审批过了!');
            };
            $userInfo           = model('users')->find($user['id']);
            $data['allow_name'] = $userInfo['real_name'];
            $data['allow_time'] = time();
            $data['allow_user'] = $user['id'];
            $ret                = Db('order')
                ->where('order_number', $data['order_number'])
                ->update($data);
            if ($ret) {
                $order_info    =    Db::name('order')->where('order_number',$data['order_number'])->find();
                $applyUserInfo = Db::name('users')->where('id',$order_info['user_id'])->find();
                $priceInfo =  Db::name('price')->where('id',$order_info['price_id'])->find();
                $expressInfo = Db::name('users')->where('id',$priceInfo['user_id'])->find();
                $wechatMessage =new Message();
                if($data['status'] ==5 ){
                   $wechatMessage->SuccessToApply($applyUserInfo['openid'],$applyUserInfo['real_name'],$order_info['goods']);
                   $wechatMessage->SuccessToApply($expressInfo['openid'],$applyUserInfo['real_name'],$order_info['goods']);
                }else{
                    $wechatMessage->RefuseToApply($applyUserInfo['openid'],$applyUserInfo['real_name'],$order_info['goods']);
                    $wechatMessage->RefuseToApply($expressInfo['openid'],$applyUserInfo['real_name'],$order_info['goods']);
                }
                $this->success('审批成功', 'wechat/index/index');
            } else {
                $this->error('操作失败!请稍后重试.');
            }
            return input('post.');
        }
        $orderInfo = model('order')
            ->where('order_number', input('id'))
            ->find();
        $priceInfo = model('price')
            ->where('id', $orderInfo['price_id'])
            ->find();
        $viewDate  = [
            'order' => $orderInfo,
            'price' => $priceInfo
        ];
        $this->assign($viewDate);
        return view();
    }

    /**
     * @return \think\response\View
     * @method 已经审批的列表
     */
    public function AllowedList()
    {
        $orderInfo = model('order')
            ->where('status', '>',4)
            ->order('create_time','desc')
            ->select();
        $viewData  = [
            'order' => $orderInfo
        ];
        $this->assign($viewData);
        return view();
    }

    public function AllowedDetails(){
        $orderInfo = model('order')
            ->where('order_number', input('id'))
            ->find();
        $priceInfo = model('price')
            ->where('id', $orderInfo['price_id'])
            ->find();
        $viewDate  = [
            'order' => $orderInfo,
            'price' => $priceInfo
        ];
        $this->assign($viewDate);
        return view();
    }
}
